package cn.web.web_module.util;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.bouncycastle.asn1.x500.style.RFC4519Style;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

import static org.bouncycastle.asn1.x500.style.RFC4519Style.o;

/**
 * @Author: 张松
 * @Description: 我的工具类
 * @Date: Created in 15:40 2020/7/25
 * @Modified By:
 */
public class MyUtil {

    /**
     *
     * @Description: 根据地址获取经纬度
     *
     * @auther: 张松
     * @date: 16:08 2020/7/25
     * @param: [address]
     * @return: java.util.Map<java.lang.String,java.lang.Double>
     *
     */
    public static Map<String,Double> getLngAndLat(String address){
        Map<String,Double> map=new HashMap<String, Double>();
        String url = "http://api.map.baidu.com/geocoder/v2/?address="+address+"&output=json&ak=14fN9rWBjnBw4LGzGe7PVNtf";
        String json = loadJSON(url);
        JSONObject obj = JSONObject.fromObject(json);
        if(obj.get("status").toString().equals("0")){
            double lng=obj.getJSONObject("result").getJSONObject("location").getDouble("lng");
            double lat=obj.getJSONObject("result").getJSONObject("location").getDouble("lat");
            map.put("lng", lng);
            map.put("lat", lat);
            //System.out.println("经度："+lng+"---纬度："+lat);
        }else{
            System.out.println("未找到相匹配的经纬度！");
        }
        return map;
    }

    /**
     *
     * @Description: 根据传输进来的地址获取json字符串
     *
     * @auther: 张松
     * @date: 16:09 2020/7/25
     * @param: [url]
     * @return: java.lang.String
     *
     */
    public static String loadJSON (String url) {
        StringBuilder json = new StringBuilder();
        try {
            URL oracle = new URL(url);
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    yc.getInputStream()));
            String inputLine = null;
            while ( (inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
        return json.toString();
    }

    /**
     *
     * @Description: 根据起点和终点经纬度获取
     *
     * @auther: 张松
     * @date: 17:49 2020/7/25
     * @param: [sourceLat, sourceLon, targetLat, targetLon]
     * @return: java.lang.Double
     *
     */
    public static Double getActualDistance(Double sourceLat,Double sourceLon,Double targetLat,Double targetLon){
        double distance=0;
        String url = "http://api.map.baidu.com/directionlite/v1/driving?origin="+sourceLat+","+sourceLon+"&destination="+targetLat+","+targetLon+"&ak=Pwp5OHSwqsePg7t8DpMNqeGTu5btUvYn";
        String json = loadJSON(url);
        JSONObject obj = JSONObject.fromObject(json);
        if (obj.get("status").toString().equals("0")){
            double aDouble = obj.getJSONObject("result").getJSONArray("routes").getJSONObject(0).getDouble("distance");
            System.out.println(aDouble);
        }
        return distance;
    }


}
